﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using DevToolkit.CustomClasses;

namespace DevToolkit
{
    public partial class EditWindow : Form
    {
        ////////////////////////////////////////////////////////////////////////////////////////////////
        // Data members
        ////////////////////////////////////////////////////////////////////////////////////////////////
        RequiredInfo info;
        string packageLocation = string.Empty;
        string removeableDrive = string.Empty;

        public RequiredInfo Info
        {
            get { return info; }
            set { info = value; LoadSettings(null); }
        }

        #region Constructor
        public EditWindow(string packageLoc = "", string removeDrive = "")
        {
            InitializeComponent();
            CategoryComboBox.Items.Add("Accessibility");
            CategoryComboBox.Items.Add("Development");
            CategoryComboBox.Items.Add("Education");
            CategoryComboBox.Items.Add("Games");
            CategoryComboBox.Items.Add("Graphics & Pictures");
            CategoryComboBox.Items.Add("Internet");
            CategoryComboBox.Items.Add("Music & Video");
            CategoryComboBox.Items.Add("Office");
            CategoryComboBox.Items.Add("Security");
            CategoryComboBox.Items.Add("Utilities");
            CategoryComboBox.SelectedItem = "Accessibility";

            LanguageComboBox.Items.Add("Multilingual");
            LanguageComboBox.Items.Add("Afrikaans");
            LanguageComboBox.Items.Add("Albanian");
            LanguageComboBox.Items.Add("Arabic");
            LanguageComboBox.Items.Add("Armenian");
            LanguageComboBox.Items.Add("Basque");
            LanguageComboBox.Items.Add("Belarusian");
            LanguageComboBox.Items.Add("Bosnian");
            LanguageComboBox.Items.Add("Breton");
            LanguageComboBox.Items.Add("Bulgarian");
            LanguageComboBox.Items.Add("Catalan");
            LanguageComboBox.Items.Add("Cibemba");
            LanguageComboBox.Items.Add("Croatian");
            LanguageComboBox.Items.Add("Czech");
            LanguageComboBox.Items.Add("Danish");
            LanguageComboBox.Items.Add("Dutch");
            LanguageComboBox.Items.Add("Efik");
            LanguageComboBox.Items.Add("English");
            LanguageComboBox.Items.Add("EnglishGB");
            LanguageComboBox.Items.Add("Esperanto");
            LanguageComboBox.Items.Add("Estonian");
            LanguageComboBox.Items.Add("Farsi");
            LanguageComboBox.Items.Add("Finnish");
            LanguageComboBox.Items.Add("French");
            LanguageComboBox.Items.Add("Galician");
            LanguageComboBox.Items.Add("Georgian");
            LanguageComboBox.Items.Add("German");
            LanguageComboBox.Items.Add("Greek");
            LanguageComboBox.Items.Add("Hebrew");
            LanguageComboBox.Items.Add("Hungarian");
            LanguageComboBox.Items.Add("Icelandic");
            LanguageComboBox.Items.Add("Igbo");
            LanguageComboBox.Items.Add("Indonesian");
            LanguageComboBox.Items.Add("Irish");
            LanguageComboBox.Items.Add("Italian");
            LanguageComboBox.Items.Add("Japanese");
            LanguageComboBox.Items.Add("Khmer");
            LanguageComboBox.Items.Add("Korean");
            LanguageComboBox.Items.Add("Kurdish");
            LanguageComboBox.Items.Add("Latvian");
            LanguageComboBox.Items.Add("Lithuanian");
            LanguageComboBox.Items.Add("Luxembourgish");
            LanguageComboBox.Items.Add("Macedonian");
            LanguageComboBox.Items.Add("Malagasy");
            LanguageComboBox.Items.Add("Malay");
            LanguageComboBox.Items.Add("Mongolian");
            LanguageComboBox.Items.Add("Norwegian");
            LanguageComboBox.Items.Add("NorwegianNynorsk");
            LanguageComboBox.Items.Add("Pashto");
            LanguageComboBox.Items.Add("Polish");
            LanguageComboBox.Items.Add("Portuguese");
            LanguageComboBox.Items.Add("PortugueseBR");
            LanguageComboBox.Items.Add("Romanian");
            LanguageComboBox.Items.Add("Russian");
            LanguageComboBox.Items.Add("Serbian");
            LanguageComboBox.Items.Add("SerbianLatin");
            LanguageComboBox.Items.Add("SimpChinese");
            LanguageComboBox.Items.Add("Slovak");
            LanguageComboBox.Items.Add("Slovenian");
            LanguageComboBox.Items.Add("Spanish");
            LanguageComboBox.Items.Add("SpanishInternational");
            LanguageComboBox.Items.Add("Swahili");
            LanguageComboBox.Items.Add("Swedish");
            LanguageComboBox.Items.Add("Thai");
            LanguageComboBox.Items.Add("TradChinese");
            LanguageComboBox.Items.Add("Turkish");
            LanguageComboBox.Items.Add("Ukrainian");
            LanguageComboBox.Items.Add("Uzbek");
            LanguageComboBox.Items.Add("Valencian");
            LanguageComboBox.Items.Add("Vietnamese");
            LanguageComboBox.Items.Add("Welsh");
            LanguageComboBox.Items.Add("Yoruba");

            // Default the the language to multilingual
            LanguageComboBox.SelectedItem = "Multilingual";

            ReleaseCombobox.Items.Add("Development Test");
            ReleaseCombobox.Items.Add("Pre-Release");
            ReleaseCombobox.Items.Add("Official Release");
            ReleaseCombobox.Items.Add("Revision");
            ReleaseCombobox.SelectedItem = "Development Test";


            NetFrameworkCombobox.Items.Add("None");
            NetFrameworkCombobox.Items.Add(".Net Framework 1.1");
            NetFrameworkCombobox.Items.Add(".Net Framework 2.0");
            NetFrameworkCombobox.Items.Add(".Net Framework 3.0");
            NetFrameworkCombobox.Items.Add(".Net Framework 3.5");
            NetFrameworkCombobox.Items.Add(".Net Framework 4.0");
            NetFrameworkCombobox.Items.Add(".Net Framework 4.5");
            NetFrameworkCombobox.SelectedItem = "None";

            this.removeableDrive = removeDrive;
            this.packageLocation = packageLoc;
        } 
        #endregion


        #region Status Label Functions
        private void SettingStatus_Reset(object sender, EventArgs e)
        {
            settingsLabel.Text = string.Empty;
        }

        private void OkButton_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Saves the information and closes the window";
        }

        private void CancelButton_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Closes the window without saving any information";
        }

        private void SaveButton_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Saves the information without closing the window";
        }

        private void AppNameTextBox_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Application Name which can contain portable at the end";
        }

        private void AppIDTextBox_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Global Unique ID (GUID) for the application, or just the name without spaces";
        }

        private void PublisherTextBox_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Publisher of this application, and those who written the application";
        }

        private void HomepageTextBox_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Homepage of the portable application, not the base application";
        }

        private void DescriptionTextBox_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Briefly describes the application. Maximum of 512 characters";
        }

        private void CategoryComboBox_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Indicates what the application falls into within the PortableApps.com Platform";
        }

        private void LanguageComboBox_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Indicates what language the portable application supports";
        }

        private void TrademarksTextBox_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Notification of any trademarks associated with the application";
        }

        private void ShareableCheckbox_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Check the box if the application is allowed to be copied from one drive to another without the Data folder";
        }

        private void OpenSourceCheckbox_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Check the box if the application is fully open source under an OSI approved license";
        }

        private void FreewareCheckbox_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Check the box if the application is free";
        }

        private void CommercialUseCheckbox_MouseEnter(object sender, EventArgs e)
        {
            settingsLabel.Text = "Check the box if the application can be used in a commercial environment";
        } 
        #endregion


        #region Other Functions
        private void EditWindow_HelpRequested(object sender, HelpEventArgs hlpevent)
        {

        }

        private void ReleaseCombobox_SelectionChanged(object sender, EventArgs e)
        {
            if (ReleaseCombobox.SelectedItem.ToString() == "Official Release")
                DisplayNumeric1.Enabled = false;
            else
                DisplayNumeric1.Enabled = true;
        } 
        #endregion


        #region Saving, Loading, and Button Clicks
        private void SaveButton_Click(object sender, EventArgs e)
        {
            // Save the information
            info.appname = AppNameTextBox.Text;
            info.appid = AppIDTextBox.Text;
            info.homepage = HomepageTextBox.Text;
            info.publisher = PublisherTextBox.Text;
            info.trademarks = TrademarksTextBox.Text;
            info.description = DescriptionTextBox.Text;
            info.category = CategoryComboBox.SelectedItem.ToString();
            info.language = LanguageComboBox.SelectedItem.ToString();

            if (NetFrameworkCombobox.SelectedItem.ToString() != "None")
                info.dotnet = NetFrameworkCombobox.SelectedItem.ToString().Substring(NetFrameworkCombobox.SelectedItem.ToString().Length - 3);
            else
                info.dotnet = NetFrameworkCombobox.SelectedItem.ToString();

            info.java = JavaCheckbox.Checked.ToString().ToLower();

            if (GhostscriptCheckbox.Checked == true)
                info.ghostscript = "yes";
            else
                info.ghostscript = "no";

            info.freeware = FreewareCheckBox.Checked.ToString().ToLower();
            info.sharable = ShareableCheckbox.Checked.ToString().ToLower();
            info.opensource = OpenSourceCheckbox.Checked.ToString().ToLower();
            info.commercialuse = CommercialCheckBox.Checked.ToString().ToLower();
            info.eulaVersion = EulaVersionTextbox.Text;
            info.pluginspath = PluginsTextbox.Text;
            info.packageVersion = PackageNumeric1.Value.ToString() + "." + PackageNumeric2.Value.ToString() + "." + PackageNumeric3.Value.ToString() + "." + PackageNumeric4.Value.ToString();
            info.displayVersion = DisplayTextbox.Text + " " + ReleaseCombobox.SelectedItem.ToString() + " " + DisplayNumeric1.Value.ToString();

            WriteSettingsToIniFile();
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            // Save the data
            SaveButton_Click(sender, e);
        }

        private void LoadSettings(object sender)
        {
            AppNameTextBox.Text = info.appname;
            AppIDTextBox.Text = info.appid;
            HomepageTextBox.Text = info.homepage;
            PublisherTextBox.Text = info.publisher;
            TrademarksTextBox.Text = info.trademarks;
            DescriptionTextBox.Text = info.description;
            CategoryComboBox.SelectedItem = info.category;
            LanguageComboBox.SelectedItem = info.language;

            if (info.dotnet == "None")
                NetFrameworkCombobox.SelectedItem = info.dotnet;
            else
                NetFrameworkCombobox.SelectedItem = ".Net Framework " + info.dotnet;

            JavaCheckbox.Checked = string.Equals(info.java.ToLower(), "True".ToLower());
            GhostscriptCheckbox.Checked = string.Equals(info.ghostscript.ToLower(), "yes".ToLower());
            FreewareCheckBox.Checked = string.Equals(info.freeware.ToLower(), "True".ToLower());
            ShareableCheckbox.Checked = string.Equals(info.sharable.ToLower(), "True".ToLower());
            OpenSourceCheckbox.Checked = string.Equals(info.opensource.ToLower(), "True".ToLower());
            CommercialCheckBox.Checked = string.Equals(info.commercialuse.ToLower(), "True".ToLower());
            EulaVersionTextbox.Text = info.eulaVersion;
            PluginsTextbox.Text = info.pluginspath;

            string[] packVer = info.packageVersion.Split('.');
            int temp = 0;
            Int32.TryParse(packVer[0], out temp);
            PackageNumeric1.Value = temp;
            Int32.TryParse(packVer[1], out temp);
            PackageNumeric2.Value = temp;
            Int32.TryParse(packVer[2], out temp);
            PackageNumeric3.Value = temp;
            Int32.TryParse(packVer[3], out temp);
            PackageNumeric4.Value = temp;

            if (info.displayVersion.Contains("Development Test") == true)
                ReleaseCombobox.SelectedItem = "Development Test";
            if (info.displayVersion.Contains("Pre-Release") == true)
                ReleaseCombobox.SelectedItem = "Pre-Release";
            if (info.displayVersion.Contains("Official Release") == true)
                ReleaseCombobox.SelectedItem = "Official Release";
            if (info.displayVersion.Contains("Revision") == true)
                ReleaseCombobox.SelectedItem = "Revision";

            string[] dispVar = info.displayVersion.Split(' ');
            DisplayTextbox.Text = dispVar[0];
            if (Int32.TryParse(dispVar[2], out temp) == false)
                Int32.TryParse(dispVar[3], out temp);
            DisplayNumeric1.Value = temp;
        }

        private void WriteSettingsToIniFile(object sender = null)
        {
            IniFile file = new IniFile(packageLocation + @"/App/AppInfo/appinfo.ini");

            IniFile launcher;
            if (info.appname.EndsWith("Portable") != true)
            {
                launcher = new IniFile(packageLocation + @"/App/AppInfo/Launcher/" + info.appname + "Portable.ini");
                launcher.WriteValue("ProgramExecutable", info.appname + "/" + info.appname + "Portable.exe", "Launch");
            }
            else
            {
                launcher = new IniFile(packageLocation + @"/App/AppInfo/Launcher/" + info.appname + ".ini");
                launcher.WriteValue("ProgramExecutable", info.appname.Substring(0, info.appname.Length - 8) + "/" + info.appname + ".exe", "Launch");
            }

            file.WriteValue("Type", "PortableApps.comFormat", "Format");
            file.WriteValue("Version", "3.0", "Format");

            file.WriteValue("Name", info.appname, "Details");
            file.WriteValue("AppID", info.appid, "Details");
            file.WriteValue("Publisher", info.publisher, "Details");
            file.WriteValue("Homepage", info.homepage, "Details");
            file.WriteValue("Category", info.category, "Details");
            file.WriteValue("Description", info.description, "Details");
            file.WriteValue("Language", info.language, "Details");
            file.WriteValue("Trademarks", info.trademarks, "Details");

            file.WriteValue("Shareable", info.sharable, "License");
            file.WriteValue("OpenSource", info.opensource, "License");
            file.WriteValue("Freeware", info.freeware, "License");
            file.WriteValue("CommercialUse", info.commercialuse, "License");
            file.WriteValue("EULAVersion", info.eulaVersion, "License");

            file.WriteValue("PackageVersion", info.packageVersion, "Version");
            file.WriteValue("DisplayVersion", info.displayVersion, "Version");

            file.WriteValue("Plugins", info.pluginspath, "SpecialPaths");
            file.WriteValue("UsesGhostscript", info.ghostscript, "Dependencies");
            file.WriteValue("UsesJava", info.java, "Dependencies");
            file.WriteValue("UsesDotNetVersion", info.dotnet, "Dependencies");

            file.WriteValue("Icons", "1", "Control");

            if (info.appname.EndsWith("Portable") != true)
            {
                launcher = new IniFile(packageLocation + @"/App/AppInfo/appinfo.ini");
                launcher.WriteValue("Start", info.appname + "Portable.exe", "Control");
            }
            else
            {
                launcher = new IniFile(packageLocation + @"/App/AppInfo/appinfo.ini");
                launcher.WriteValue("Start", info.appname + ".exe", "Control");
            }

            file.Format();
        } 
        #endregion
    }    
}
